<?php

namespace App\Http\Requests\School;

use App\Models\BookRule;
use App\Models\Food;
use Illuminate\Foundation\Http\FormRequest;

/**
 * Class StoreBookRule
 * @package App\Http\Requests\School
 * 新增订餐规则
 */
class StoreBookRule extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'eat_date' => 'required',
            'eat_type' => [
                'required',
                function ($attribute, $value, $fail) {
                    if ($value === BookRule::EAT_TYPE_BREAKFAST) {
                        if (BookRule::where('school_id', \Auth::user()->school_id)->where('eat_date', $this->eat_date)->isBreakfast()->exists()) {
                            // 若存在订餐类型为早餐并且订餐时间又相同的话
                            return $fail('此时间段已存在订餐类型为早餐的订餐规则');
                        }
                    }
                    if ($value === BookRule::EAT_TYPE_LUNCH) {
                        if (BookRule::where('school_id', \Auth::user()->school_id)->where('eat_date', $this->eat_date)->isLunch()->exists()) {
                            return $fail('此时间段已存在订餐类型为午餐的订餐规则');
                        }
                    }
                    if ($value === BookRule::EAT_TYPE_DINNER) {
                        if (BookRule::where('school_id', \Auth::user()->school_id)->where('eat_date', $this->eat_date)->isDinner()->exists()) {
                            return $fail('此时间段已存在订餐类型为晚餐的订餐规则');
                        }
                    }
                }
            ],
            'food_ids' => [
                'required',
                'array',
                function ($attribute, $value, $fail) {
                    foreach ($value as $item) {
                        if (gettype($item) === 'string') {
                            $query = Food::query()
                                ->where('status', Food::START_USING)
                                ->where('school_id', \Auth::user()->school_id)
                                ->where('name', $item);
                            if ($query->exists()) {
                                return $fail($item . ' 该菜品已存在');
                            }
                        }
                    }
                }
            ],
        ];
    }

    /**
     * @return array
     */
    public function attributes()
    {
        return [
            'eat_type' => '订餐类型',
            'food_ids' => '菜品',
            'eat_date' => '订餐时间'
        ];
    }

    /**
     * @return array
     */
    public function messages()
    {
        return [
            'eat_type.required' => '请选择:attribute',
            'food_ids.required' => '请选择:attribute',
            'food_ids.array' => ':attribute参数错误',
            'eat_date.required' => '请选择:attribute'
        ];
    }
}
